﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace SysRouteLinq.Forms
{
    public partial class ConsultaLinha : Form
    {
        private SysRouteDataContext dc;
        private bool result = false;

        public ConsultaLinha()
        {
            dc = new SysRouteDataContext();
            InitializeComponent();
        }

        private void ConsultaLinha_Shown(object sender, EventArgs e)
        {
            fillComboBox();
        }

        private void fillComboBox()
        {
            IEnumerable<Linha> l = from f in dc.Linhas
                                   where f.id_empresa != null
                                   orderby
                                       f.numero
                                   select f;

            comboBox1.DataSource = l.ToList();
            comboBox1.DisplayMember = "numero";
            comboBox1.ValueMember = "id_linha";

            Linha l2 = (from f in dc.Linhas
                        where f == comboBox1.SelectedItem
                        select f).Single();

            textBox2.Text = l2.descricao;
            textBox3.Text = l2.referencia;
            textBox4.Text = l2.Empresa.nome;

            result = true;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            Linha r = (from f in dc.Linhas
                        where
                        f == comboBox1.SelectedItem
                        select f).Single();

            IEnumerable<LinhaParada> p = (from f2 in dc.LinhaParadas
                                          where
                                             f2.id_linha == r.id_linha
                                          select f2);

            foreach (LinhaParada i in p)
            {
                dc.LinhaParadas.DeleteOnSubmit(i);
            }

            dc.Linhas.DeleteOnSubmit(r);
            dc.SubmitChanges();
        }

        private void comboBox1_SelectedValueChanged(object sender, EventArgs e)
        {
            if (result == true)
            {
                Linha r = (from f in dc.Linhas
                            where f == comboBox1.SelectedItem
                            select f).Single();

                textBox2.Text = r.descricao;
                textBox3.Text = r.referencia;
                textBox4.Text = r.Empresa.nome;
            }
            else { }
        }

    }
}
